<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Redis AOF 重写机制深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
            border: 1px solid transparent;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            border-color: #667eea;
        }
        
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 100%;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.2rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 15px;
            color: white;
            font-size: 24px;
            margin-bottom: 1rem;
        }
        
        .timeline-item {
            position: relative;
            padding-left: 40px;
        }
        
        .timeline-item::before {
            content: '';
            position: absolute;
            left: 10px;
            top: 0;
            bottom: -30px;
            width: 2px;
            background: #e5e7eb;
        }
        
        .timeline-item:last-child::before {
            display: none;
        }
        
        .timeline-dot {
            position: absolute;
            left: 0;
            top: 8px;
            width: 20px;
            height: 20px;
            background: white;
            border: 3px solid #667eea;
            border-radius: 50%;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        code {
            background: #f3f4f6;
            padding: 0.2rem 0.5rem;
            border-radius: 0.25rem;
            font-family: 'Consolas', 'Monaco', monospace;
            color: #764ba2;
            font-size: 0.9em;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6">
                    Redis AOF 重写机制
                </h1>
                <p class="text-xl md:text-2xl opacity-90 mb-8">
                    深入理解 Redis 持久化优化的核心技术
                </p>
                <div class="flex justify-center space-x-6 text-lg">
                    <div class="flex items-center">
                        <i class="fas fa-compress-alt mr-2"></i>
                        <span>文件压缩</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-tachometer-alt mr-2"></i>
                        <span>性能优化</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-shield-alt mr-2"></i>
                        <span>数据安全</span>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12 max-w-6xl">
        <!-- Introduction Card -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">A</span>OF 重写是 Redis 维护和优化 AOF 文件的关键过程。通过压缩和清理 AOF 文件，它能够减少文件大小、提高恢复速度和改善性能。这是确保 Redis 持久化系统高效且可靠运行的重要机制。
            </p>
        </div>

        <!-- AOF File Growth Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 section-title">AOF 文件增长问题</h2>
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="feature-icon">
                        <i class="fas fa-database"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">AOF 文件的作用</h3>
                    <p class="text-gray-600 leading-relaxed">
                        AOF 文件记录了 Redis 执行的所有写命令，确保了数据的持久化。每当有写操作发生时，Redis 会将对应的命令追加到 AOF 文件中。
                    </p>
                </div>
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="feature-icon">
                        <i class="fas fa-chart-line"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">文件大小增长</h3>
                    <p class="text-gray-600 leading-relaxed">
                        随着时间的推移，AOF 文件会不断增长。这会导致文件变得非常大，从而影响加载速度、磁盘空间和 I/O 性能。
                    </p>
                </div>
            </div>
        </section>

        <!-- Purpose of AOF Rewrite -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 section-title">AOF 重写的目的</h2>
            <p class="text-lg text-gray-700 mb-8">
                <strong>AOF 重写（AOF Rewrite）</strong> 是一个优化过程，用于压缩和清理 AOF 文件。主要目的包括：
            </p>
            
            <div class="space-y-8">
                <!-- Reduce File Size -->
                <div class="bg-gradient-to-r from-purple-50 to-indigo-50 rounded-xl p-8">
                    <h3 class="text-2xl font-bold mb-4 text-gradient">
                        <i class="fas fa-compress mr-3"></i>减少 AOF 文件的大小
                    </h3>
                    <div class="grid md:grid-cols-2 gap-6">
                        <div class="bg-white rounded-lg p-6">
                            <h4 class="font-semibold text-lg mb-2 text-purple-700">
                                <i class="fas fa-trash-alt mr-2"></i>删除冗余命令
                            </h4>
                            <p class="text-gray-600">
                                AOF 文件中可能包含重复的或过时的命令。重写过程会根据当前数据库的状态生成一个新的 AOF 文件，只包含最简洁的命令集。
                            </p>
                        </div>
                        <div class="bg-white rounded-lg p-6">
                            <h4 class="font-semibold text-lg mb-2 text-purple-700">
                                <i class="fas fa-file-archive mr-2"></i>压缩数据
                            </h4>
                            <p class="text-gray-600">
                                通过删除不必要的命令，重写过程将 AOF 文件压缩到最小，节省磁盘空间。
                            </p>
                        </div>
                    </div>
                </div>

                <!-- Improve Recovery Speed -->
                <div class="bg-gradient-to-r from-blue-50 to-cyan-50 rounded-xl p-8">
                    <h3 class="text-2xl font-bold mb-4 text-gradient">
                        <i class="fas fa-rocket mr-3"></i>提高恢复速度
                    </h3>
                    <div class="grid md:grid-cols-2 gap-6">
                        <div class="bg-white rounded-lg p-6">
                            <h4 class="font-semibold text-lg mb-2 text-blue-700">
                                <i class="fas fa-download mr-2"></i>加载速度
                            </h4>
                            <p class="text-gray-600">
                                较小的 AOF 文件意味着 Redis 在重启时加载 AOF 文件的速度更快。重写后的 AOF 文件只包含必要的命令，因此恢复速度更快。
                            </p>
                        </div>
                        <div class="bg-white rounded-lg p-6">
                            <h4 class="font-semibold text-lg mb-2 text-blue-700">
                                <i class="fas fa-clock mr-2"></i>减少启动时间
                            </h4>
                            <p class="text-gray-600">
                                重写后的文件更易于处理，可以显著减少 Redis 启动时的加载时间。
                            </p>
                        </div>
                    </div>
                </div>

                <!-- Improve Performance -->
                <div class="bg-gradient-to-r from-green-50 to-emerald-50 rounded-xl p-8">
                    <h3 class="text-2xl font-bold mb-4 text-gradient">
                        <i class="fas fa-bolt mr-3"></i>改善性能
                    </h3>
                    <div class="grid md:grid-cols-2 gap-6">
                        <div class="bg-white rounded-lg p-6">
                            <h4 class="font-semibold text-lg mb-2 text-green-700">
                                <i class="fas fa-hdd mr-2"></i>减少磁盘 I/O
                            </h4>
                            <p class="text-gray-600">
                                较小的 AOF 文件减少了磁盘 I/O 操作，这有助于提高 Redis 的性能。
                            </p>
                        </div>
                        <div class="bg-white rounded-lg p-6">
                            <h4 class="font-semibold text-lg mb-2 text-green-700">
                                <i class="fas fa-memory mr-2"></i>优化内存使用
                            </h4>
                            <p class="text-gray-600">
                                压缩后的 AOF 文件减少了内存占用，尤其是在加载大文件时。
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Implementation Process -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 section-title">AOF 重写的实现</h2>
            <p class="text-lg text-gray-700 mb-8">
                <strong>AOF 重写的过程</strong> 通常包括以下步骤：
            </p>

            <!-- Process Diagram -->
            <div class="bg-white rounded-xl shadow-lg p-8 mb-8">
                <div class="mermaid">
                    graph TD
                        A[检测触发条件] --> B{是否需要重写?}
                        B -->|是| C[创建子进程]
                        B -->|否| A
                        C --> D[扫描当前数据库]
                        D --> E[生成新AOF文件]
                        E --> F[主进程继续处理命令]
                        F --> G[子进程完成重写]
                        G --> H[替换旧AOF文件]
                        H --> I[删除旧文件]
                        I --> J[重写完成]
                        
                        style A fill:#f9f,stroke:#333,stroke-width:2px
                        style J fill:#9f9,stroke:#333,stroke-width:2px
                        style C fill:#bbf,stroke:#333,stroke-width:2px
                </div>
            </div>

            <!-- Timeline -->
            <div class="space-y-8">
                <div class="timeline-item">
                    <div class="timeline-dot"></div>
                    <div class="bg-white rounded-lg shadow-md p-6">
                        <h3 class="text-xl font-bold mb-3 text-purple-700">
                            <i class="fas fa-play